package com.yan.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 日期工具类
 *
 * @author Administrator
 */
public class DateUtil {

    /**
     * 日期对象转字符串
     *
     * @param date
     * @param format
     * @return
     */
    public String formatDate(Date date, String format) {
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if (date != null) {
            result = sdf.format(date);
        }
        return result;
    }

    /**
     * 字符串转日期对象
     *
     * @param str
     * @param format
     * @return
     * @throws Exception
     */
    public static Date formatString(String str, String format) throws Exception {

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(str);
    }

    public static String getCurrentDateStr() throws Exception {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        return sdf.format(date);
    }

    private static ThreadLocal<Map<String, SimpleDateFormat>> sdfMap = new ThreadLocal<Map<String, SimpleDateFormat>>() {
        @Override
        protected Map<String, SimpleDateFormat> initialValue() {
            System.out.println(Thread.currentThread().getName() + " init pattern: " + Thread.currentThread());
            return new HashMap<String, SimpleDateFormat>();
        }
    };

    /**
     * 返回一个SimpleDateFormat,每个线程只会new一次pattern对应的sdf
     *
     * @param pattern
     * @return
     */
    private static SimpleDateFormat getSdf(final String pattern) {
        Map<String, SimpleDateFormat> tl = sdfMap.get();
        SimpleDateFormat sdf = tl.get(pattern);
        if (sdf == null) {
            System.out.println(Thread.currentThread().getName() + " put new sdf of pattern " + pattern + " to map");
            sdf = new SimpleDateFormat(pattern);
            tl.put(pattern, sdf);
        }
        return sdf;
    }

    /**
     * 这样每个线程只会有一个SimpleDateFormat
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        return getSdf(pattern).format(date);
    }

    public static Date parse(String dateStr, String pattern) throws ParseException {
        return getSdf(pattern).parse(dateStr);
    }

    /**
     * 字符串时间转换为Unix时间戳
     */
    public static String date2TimeStamp(String format, String dateString) {

        try {
            Date date = new SimpleDateFormat(format).parse(dateString);
            long unixTimestamp = date.getTime() / 1000;
            return String.valueOf(unixTimestamp);
        } catch (ParseException e) {
            e.getMessage();
        }
        return "";
    }


    /**
     * Unix时间戳转换成字符串时间
     */
    public static String timeStamp2Date(String format, String timestampString) {
        Long timestamp = Long.parseLong(timestampString) * 1000;
        String date = new java.text.SimpleDateFormat(format).format(new java.util.Date(timestamp));
        return date;
    }


    /**
     * 计算两个时间间隔值
     * type=1 表示去除首尾时间
     * type为其他值时不去除首尾时间
     */
    public static List<String> getBetweenDates(String begin, String end, int type) throws ParseException {
        List<String> result = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = sdf.parse(begin);
        Date endTime = sdf.parse(end);

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(sdf.parse(begin));

        if (1 == type) {
            Calendar tempEnd = Calendar.getInstance();
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
            tempEnd.setTime(sdf.parse(end));
            while (tempStart.before(tempEnd)) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } else {
            while (startTime.getTime() <= endTime.getTime()) {
                result.add(sdf.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
                startTime = tempStart.getTime();
            }
        }

        return result;
    }

}
